/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Profile, SchemaFrom, SchemaInfo, SchemaItem } from "@/components/JsonSchemaForm/interface";
import { SchemaNodeKey, SchemaNodeType } from "@/utils/constants";
import jsonpath from "jsonpath";

/**
 * 解析Schema
 */
//获取basic模型
export function parseSchemaFromBasic(schema: SchemaItem) {
	// let basic = schema.properties.props.properties[SchemaNodeKey.basic].properties;
	let basic = jsonpath.query(schema, `$..${SchemaNodeKey.basic}.${SchemaNodeKey.properties}`);
	return JSON.parse(JSON.stringify(basic[0] || {}));
}
export function parseSchemaFromSystem(schema: SchemaItem) {
	// let basic = schema.properties.props.properties[SchemaNodeKey.basic].properties;
	let system = jsonpath.query(schema, `$..${SchemaNodeKey.schema}.${SchemaNodeKey.properties}`);
	return JSON.parse(JSON.stringify(system[0] || {}));
}

//获取配置模型
export function parseSchemaFromConfig(schema: SchemaItem) {
	let config = schema.properties.props.properties[SchemaNodeKey.config].properties;
	return JSON.parse(JSON.stringify(config));
}
//获取配置模型
export function parseSchemaFromSubdevice(schema: SchemaItem) {
	// let subdevice = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.subdevice];
	let subdevice = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.subdevice];
	return JSON.parse(JSON.stringify(subdevice));
}
//获取配置模型
export function parseSchemaFromSubdeviceParams(schema: SchemaItem) {
	// let subdevice = schema.properties[SchemaNodeKey.config].properties[SchemaNodeKey.subdevice]?.properties;
	let subdevice = jsonpath.query(schema, `$..${SchemaNodeKey.config}..${SchemaNodeKey.subdevice}.properties`);
	return JSON.parse(JSON.stringify(subdevice[0] || {}));
}
//获取配置模型
export function parseSchemaFromGateway(schema: SchemaItem) {
	// let gatewayParam = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.gateway].properties;
	let gatewayParam = jsonpath.query(schema, `$..${SchemaNodeKey.config}.properties`);
	return JSON.parse(JSON.stringify(gatewayParam[0] || {}));
}
//获取配置模型
export function parseSchemaConfig(schema: SchemaItem) {
	// let gatewayParam = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.gateway].properties;
	let gatewayParam = jsonpath.query(schema, `$..${SchemaNodeKey.config}`);
	return JSON.parse(JSON.stringify(gatewayParam[0] || {}));
}
//获取网关的变量模型
export function parseSchemaFromGatewaySensor(schema: SchemaItem) {
	// let gatewayParam = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.gateway].properties;
	let gatewaySensor = jsonpath.query(
		schema,
		`$..${SchemaNodeKey.config}.properties.${SchemaNodeKey.subdevice}.properties.${SchemaNodeKey.sensor}`
	);
	return JSON.parse(JSON.stringify(gatewaySensor[0] || {}));
}
//获取变量模型
export function parseConfigSchemaFromSubdeviceSensor(schema: SchemaItem) {
	// let gatewayParam = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.gateway].properties;
	let gatewayParam = jsonpath.query(
		schema,
		`$..${SchemaNodeKey.config}..${SchemaNodeKey.subdevice}.properties.${SchemaNodeKey.sensor}.properties`
	);
	return JSON.parse(JSON.stringify(gatewayParam[0] || {}));
}
//获取变量模型
export function parseConfigSchemaFromGateway(schema: SchemaItem) {
	let gatewayParam = jsonpath.query(schema, `$..${SchemaNodeKey.config}.properties`);
	return JSON.parse(JSON.stringify(gatewayParam[0] || {}));
}
//获取变量模型
export function profileConfigGateway(schema: SchemaItem) {
	let gatewayParam = jsonpath.query(schema, `$..${SchemaNodeKey.config}`);
	return JSON.parse(JSON.stringify(gatewayParam[0] || {}));
}
//获取变量模型
export function parseConfigSchemaFromSubdevice(schema: SchemaItem) {
	// let gatewayParam = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.gateway].properties;
	let properties = jsonpath.query(schema, `$..${SchemaNodeKey.config}..${SchemaNodeKey.subdevice}.properties`);
	let gatewayParam = properties[0] || {};
	let obj: any = {};
	Object.keys(gatewayParam).map(key => {
		if (key != SchemaNodeKey.sensor) {
			obj[key] = gatewayParam[key];
		}
	});
	return JSON.parse(JSON.stringify(obj || {}));
}
//获取配置模型
export function parseSchemaFromDevice(schema: SchemaItem) {
	// let device = schema.properties.props.properties[SchemaNodeKey.config].properties[SchemaNodeKey.device];
	let device = jsonpath.query(schema, `$..${SchemaNodeKey.config}.properties`);
	return JSON.parse(JSON.stringify(device[0] || {}));
}

//获取采集量配置模型
export function parseSchemaFromSensor(schema: SchemaItem) {
	// let sensor = schema.properties[SchemaNodeKey.config].properties[SchemaNodeKey.subdevice].properties[SchemaNodeKey.sensor];
	let sensor = jsonpath.query(
		schema,
		`$..${SchemaNodeKey.config}..${SchemaNodeKey.subdevice}..${SchemaNodeKey.sensor}.${SchemaNodeKey.properties}`
	);
	return JSON.parse(JSON.stringify(sensor[0] || {}));
}
//获取采集量配置模型
export function parseDeviceSchemaFromSensor(schema: SchemaItem) {
	// let sensor = schema.properties[SchemaNodeKey.config].properties[SchemaNodeKey.subdevice].properties[SchemaNodeKey.sensor];
	let sensor = jsonpath.query(schema, `$..${SchemaNodeKey.sensor}.${SchemaNodeKey.properties}`);
	return JSON.parse(JSON.stringify(sensor[0] || {}));
}
//获取采集量配置项
export function parseSchemaFromSensorNode(schema: SchemaItem, key: string) {
	let sensor = parseSchemaFromSensor(schema);
	return JSON.parse(JSON.stringify(sensor[key]));
}

//获取功能控制模型
export function parseSchemaFromFunc(schema: SchemaItem): SchemaInfo {
	// let func = schema.properties[SchemaNodeKey.funcs].properties;
	let funcSchema = jsonpath.query(schema, `$..properties.funcs`);
	let func: SchemaInfo = funcSchema[0] || {};
	return JSON.parse(JSON.stringify(func));
}

/**
 * 解析Profile
 */
//获取sn状态
export function parseProfileFromSn(profile: Profile) {
	let basic = jsonpath.query(profile, `$..props.basic`);
	return basic[0] && basic[0].sn;
}
export function parseProfileFormBasicValue(profile: Profile, fieldKey: string) {
	let basic = jsonpath.query(profile, `$..props.basic`);
	return basic[0] && basic[0][fieldKey];
}
export function parseProfileFromRealtimeSensorValue(profile: any) {
	let sensorValues = jsonpath.query(profile, `$..${SchemaNodeKey.sensor}`);
	return JSON.parse(JSON.stringify(sensorValues[0] || {}));
}
//获取设备参数
export function parseProfileFromDevice(profile: Profile) {
	let device = jsonpath.query(profile, `$..config`);
	return JSON.parse(JSON.stringify(device[0] || {}));
}
export function parseProfileFromDeviceSensorParam(profile: Profile) {
	let sensor = jsonpath.query(profile, `$..config.${SchemaNodeKey.sensor}`);
	return JSON.parse(JSON.stringify(sensor[0] || {}));
}
//获取basic状态
export function parseProfileFromSystem(profile: Profile) {
	let system = jsonpath.query(profile, `$..props.${SchemaNodeKey.system}`);
	return JSON.parse(JSON.stringify(system[0] || {}));
}
export function parseProfileFromRuntime(profile: Profile) {
	let runtime = jsonpath.query(profile, `$..props.${SchemaNodeKey.runtime}`);
	return JSON.parse(JSON.stringify(runtime[0] || {}));
}

//获取配置模型内容
export function parseProfileFormGateway(profile: any) {
	let gateway = jsonpath.query(profile, `$..config`);
	return JSON.parse(JSON.stringify(gateway[0] || {}));
}
//获取配置模型内容
export function parseProfileFormGatewaySensor(profile: any) {
	let sensor = jsonpath.query(profile, `$..config.subdevice..${SchemaNodeKey.sensor}`);
	return JSON.parse(JSON.stringify(sensor[0] || {}));
}

//获取控制模型内容
export function parseProfileFormFunc(profile: any) {
	let funcs = jsonpath.query(profile, `$..funcs`);
	return JSON.parse(JSON.stringify(funcs[0] || {}));
}

//获取采集量配置内容
export function parseProfileFromSensor(profile: any) {
	let sensor = jsonpath.query(profile, `$..${SchemaNodeKey.sensor}`);
	if (!sensor[0]) {
		sensor = jsonpath.query(profile, `$..profile.${SchemaNodeKey.sensor}`);
	}
	return JSON.parse(JSON.stringify(sensor[0] || {}));
}

/**
 * 将Scheme节点数据转换成TreeList
 * @param schemeNode
 * @returns []
 * @example:
 * src: {
				key: {
					type: "string",
					title: "变量标识"
				},
				type: {
					type: "string",
					title: "类型"
				},
				unit: {
					type: "string",
					title: "单位"
				},
				ext: {
					type: "object",
					title: "接入参数",
					properties: {
						ip: {
							type: "string",
							title: "IP"
						},
						port: {
							type: "string",
							title: "端口"
						},
						server: {
							type: "object",
							title: "启动参数",
							properties: {
								f1: {
									type: "string",
									title: "参数1"
								},
								f2: {
									type: "string",
									title: "参数2"
								}
							}
						}
					}
				}
			}
 * result: [
        {
            "key": "key",
            "type": "string",
            "title": "变量标识",
            "children": []
        },
        {
            "key": "type",
            "type": "string",
            "title": "类型",
            "children": []
        },
        {
            "key": "unit",
            "type": "string",
            "title": "单位",
            "children": []
        },
        {
            "key": "ext",
            "type": "object",
            "title": "接入参数",
            "children": [
                {
                    "key": "ip",
                    "type": "string",
                    "title": "IP",
                    "children": []
                },
                {
                    "key": "port",
                    "type": "string",
                    "title": "端口",
                    "children": []
                },
                {
                    "key": "server",
                    "type": "object",
                    "title": "启动参数",
                    "children": [
                        {
                            "key": "f1",
                            "type": "string",
                            "title": "参数1",
                            "children": []
                        },
                        {
                            "key": "f2",
                            "type": "string",
                            "title": "参数2",
                            "children": []
                        }
                    ]
                }
            ]
        }
    ]
 */
export function parseSchemaToTreeList(schemeNode: SchemaItem) {
	function generatorTree(key: string, item: SchemaInfo, parent: any) {
		let dataInfo: any = {
			path: parent.path ? parent.path + "." + key : key,
			pathName: parent.pathName ? parent.pathName + "." + item.title : item.title || "",
			...item
		};
		if (dataInfo.type == SchemaNodeType.object) {
			dataInfo.children = [];
		}
		if (!parent.children) {
			parent.children = [];
		}
		parent.children.push(dataInfo);
		if (item.properties) {
			//获取子节点
			Object.keys(item.properties).forEach(sk => {
				generatorTree(sk, item.properties![sk], dataInfo);
			});
		}
	}
	let tableData = { children: [] };
	for (let key in schemeNode) {
		let schemaInfo = schemeNode[key];
		schemaInfo.key = schemaInfo.key || key;
		generatorTree(key, schemaInfo, tableData);
	}
	return tableData.children;
}

export function generatorParentForm(submitData: SchemaFrom): SchemaFrom {
	//循环最近父节点
	let paths = submitData.path?.split(".") || [];
	let index = 0;
	function generatorParent(childKey: string, parent: any) {
		index++;
		parent.properties[childKey] = { type: SchemaNodeType.object, properties: {} };
		if (index == paths.length - 1) {
			parent.properties[childKey].properties[submitData.key] = submitData;
			return;
		}
		if (index < paths.length) {
			let nextLevel = paths[index];
			generatorParent(nextLevel, parent.properties[childKey]);
		}
	}
	let parent: any = { type: SchemaNodeType.object, properties: {} };
	generatorParent(paths[0], parent);
	return parent;
}
